﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace zielonka.co.uk.extensions.system
{
    //  Description:	Allows enumaration of sets of characters by expressing them as a range, for example all the lowercase characters. Allows reverse order as well.
    /*
        foreach (char c in 'a'.To('z'))
				Console.WriteLine(c);
			foreach (char c in '0'.To('9'))
				Console.WriteLine(c);

     */

    public static partial class CharExtensions
    {
        private static void Swap(ref char a, ref char b)
        {
            a ^= b;
            b ^= a;
            a ^= b;
        }

        public static IEnumerable<char> ToRange(this char first, char last)
        {
            bool reverseRequired = (first > last);

            if (reverseRequired)
                Swap(ref first, ref last);

            var result = Enumerable.Range(first, last - first + 1).Select(charCode => (char)charCode);

            if (reverseRequired)
                result = result.Reverse();

            return result;
        }
    }
}
